package com.bluesky.plum.uimodels.standard.components.containers.form.commonForms;

import com.bluesky.plum.richdomain.RichDomainMethod;
import com.bluesky.plum.uimodels.standard.FieldUIComponent;
import com.bluesky.plum.uimodels.standard.UIComponentBuilder;
import com.bluesky.plum.uimodels.standard.components.EventListener;
import com.bluesky.plum.uimodels.standard.components.containers.form.Form;

/**
 * It support the method: void update(Object oldEntity, Object newEntity); once
 * the oldEntity is selected, newEntity will be refreshed.
 * 
 * @author jack
 * 
 */
public class UpdateFormCreator extends SimpleFormBuilder {

	@Override
	public Form createForm(final UIComponentBuilder uiComponentBuilder,
			final RichDomainMethod rdm) {

		final Form form = super.createForm(uiComponentBuilder, rdm);

		
		// add some events
		// default listenr
		ParameterChangedEvent parameterChangedEvent = new ParameterChangedEvent() {
			public void changed(int parameterIndex, FieldUIComponent[] components) {

				System.out.println(String.format(
						"[UpdateForm] Parameter value changed: %s,%s",
						components[parameterIndex].getName(),
						components[parameterIndex].getDomainFieldValue()));

				//Parameter2 is assigned by Parameter1
				components[1].setDomainFieldValue(components[0]
						.getDomainFieldValue());
			};
		};

		form.setParameterChangedEvent(parameterChangedEvent);

		//initialized it to 0
		form.setCreateEvent(new EventListener(){
			@Override
			public void process() {
				form.getParameterChangedEvent().changed(0, form.getFieldUiComponents());		
			}
		});
		
		
		return form;
	}
}
